package yxy.game.pm2.command.yilingbattle;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.levelrewardduplicate;
import yxy.cherry.data.bean.mapduplicate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.newDao.YiLingInfoDao;
import yxy.game.pm2.bean.db.po.YiLingBattlePo;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;
import yxy.game.pm2.module.yiling.YiLingBattleModule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/****************************************
 * @name CMD_YiLingBattleUI
 * @description 夷陵之战 界面通关玩家信息
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleUI, classtype = YiLingMessage.YiLingBattleUIRequest.class)
public class CMD_YiLingBattleUI extends Command<YiLingMessage.YiLingBattleUIRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleUIRequest request) throws Exception {
        YiLingMessage.YiLingBattleUIResponse.Builder response = YiLingMessage.YiLingBattleUIResponse.newBuilder();

        try {
            String playerCode = messageSource.getTag();


            //获取当前地图界面的id
            int currentMap = request.getCurrentMap();

            //夷陵缓存
            YiLingBattleModule module = YiLingBattleModule.get(playerCode);

            //获取所有关卡
            List<Integer> stageList = YiLingBattleHandle.getStageList(currentMap, playerCode);

            //当前玩家信息
            YiLingBattlePo yiLingBattlePo = new YiLingBattlePo();

            //获取关卡集合的第一关和最后一关
            int[] ints = YiLingBattleHandle.getStartAndEnd(stageList);

            //2.从数据库查当前地图界面的每个关卡信息玩家信息
            PlayerBean player = PlayerBean.get(playerCode);
            ServerBean serverBean = ServerBean.get(player.getServerId());
            AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
            List<YiLingBattlePo> playerList = YiLingInfoDao.getList(areaBean.getGroupId(), ints[0], ints[1]);

            JSONArray mapJson = YiLingBattleHandle.getMapJson(stageList, playerList, playerCode);

            ArrayList<Integer> levelReward = module.getLevelReward();

            levelrewardduplicate _levelreward = null;
            if (levelReward == null || levelReward.size() == 0) {
                _levelreward = DataCenter.getData(1, levelrewardduplicate.class);
            } else {
                _levelreward = DataCenter.getData(levelReward.size() + 1, levelrewardduplicate.class);
            }
            //下一礼包的奖励内容
            Collection<Currency> rewards = Currency.create(_levelreward.getRewardtype(), _levelreward.getRewardid(), _levelreward.getRewardnum());
            //下一级的领取条件
            int condition = _levelreward.getCondition();
            //当前关卡
            int mapId = module.getMapId();

            //当前账号玩家
            JSONArray heroList = new JSONArray();
            for (int i = 0; i < mapJson.size(); i++) {
                JSONObject jsonObject = (JSONObject) mapJson.get(i);
                if (jsonObject.getIntValue("mapId") == mapId) {
                    heroList = jsonObject.getJSONArray("heroList");
                }
            }
            mapduplicate mapduplicate = DataCenter.getData(mapId, yxy.cherry.data.bean.mapduplicate.class);
            if (currentMap == 0 || currentMap == mapduplicate.getState()) {
                String resultJson = YiLingBattleHandle.result(mapId, heroList, mapJson, yiLingBattlePo, player, serverBean, areaBean, module);
                response.setMapTotalJson(resultJson);
            } else {
                response.setMapTotalJson(mapJson.toJSONString());
            }


            response.addAllRndRewards(new CurrencyHandle().packItems(rewards));
            response.setCondition(condition);
            response.setMapId(mapId); //下一关卡
            response.setSweepNum(module.remainTimes()); //剩余扫荡次数
            if (module.getTimes() > 0) {
                response.setIsCost(1);
            } else {
                response.setIsCost(0); //首次免费
            }

            response.setLessChallengeTimes(YiLingBattleHandle.maxChallengeTimes - module.getMaxCustomsPass());//剩余挑战次数


//          response.setLessChallengeTimes(module.getMaxCustomsPass()-module.getTimes());
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        }
    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleUIResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
